package com.airlines.crewassignment.controller.linkline;

import java.util.Date;
import java.util.List;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.ResponseBody;

import com.airlines.base.config.response.ResponseResult;
import com.airlines.base.config.response.ResponseResultBuilder;
import com.airlines.base.entity.dict.Select2;
import com.airlines.common.ListSortUtil;
import com.airlines.common.TimeUtil;
import com.airlines.crewassignment.entity.linkline.TaskLoop;
import com.airlines.crewassignment.service.dict.Select2Service;
import com.airlines.crewassignment.service.linkline.FullCalendarService;
import com.airlines.crewassignment.service.linkline.TaskLoopFlightFactService;
import com.airlines.crewassignment.service.linkline.TaskLoopService;

import cn.mercury.utils.JsonUtils;

/**
 * @author xxxx
 * @date 2018年6月11日
 * @description 飞行员任务环管理
 */
@Controller
@RequestMapping("pilotTaskLoop")
public class PilotTaskLoopController {
	@Autowired
	private TaskLoopService taskLoopService;
	@Autowired
	private Select2Service select2Service;

	@Autowired
	private FullCalendarService fullCalendarService;
	@Autowired
	private TaskLoopFlightFactService taskLoopFlightFactService;

	@RequestMapping("toTaskLoopPage")
	public String toTaskLoopPage(String dutyCode,Model model) {
		model.addAttribute("dutyCode", dutyCode);
		return "pilotAssign/taskPage";
	}

	/**
	 * 查询所有任务环主信息
	 * 应当传入dutyCode值
	 * @param taskLoop
	 * @return
	 */
	@RequestMapping("listTaskLoop")
	@ResponseBody
	public Object listTaskLoop(TaskLoop taskLoop) {
		List<TaskLoop> list = taskLoopService.listData(taskLoop);
		ListSortUtil.sort(list, true, "updateTime");
		return (list);
	}

	/**
	 * 根据任务环id查询任务明细数据
	 * 
	 * @param id
	 * @return
	 */
	@RequestMapping("taskLoop")
	@ResponseBody
	public Object taskLoop(@RequestParam(required = true, name = "id") Integer id) {
		TaskLoop task = taskLoopService.getTaskLoopAll(id);
		if (task != null) {
			return (task);
		} else {
			return ResponseResultBuilder.errorResult(null, "此任务环不存在", "404").toString();
		}

	}

	/**
	 * 返回机组几人制下拉框
	 * 
	 * @return
	 */
	@RequestMapping("getCrewNumberType")
	@ResponseBody
	public Object getCrewNumberType(String dutyCode) {
		List<Select2> list = select2Service.getCrewNumberType(dutyCode);
		return (list);
	}

	/**
	 * 保存Json嵌套格式的任务环数据
	 * 应当保存dutyCode
	 * @param taskLoop
	 * @return
	 */
	@RequestMapping("save")
	@ResponseBody
	public ResponseResult<String> save(String data) {
		TaskLoop taskLoop = JsonUtils.fromJson(data, TaskLoop.class);
		taskLoop.setEndDate(TimeUtil.getDateDayEndTime(taskLoop.getEndDate()));
		ResponseResult<String> r = taskLoopService.saveCk(taskLoop);
		if(r.isSuccess()){
			taskLoopService.saveAll(taskLoop);
			return ResponseResultBuilder.successResult();
		}else{
			return r;
		}
		
	}

	/**
	 * 禁用任务环
	 * 
	 * @param taskLoopId
	 * @return
	 */
	@RequestMapping("disable")
	@ResponseBody
	public ResponseResult<String> disable(int taskLoopId) {
		int i = taskLoopService.disable(taskLoopId);
		if (i == 1) {
			return ResponseResultBuilder.successResult();
		} else {
			return ResponseResultBuilder.errorResult();
		}
	}

	/**
	 * 自动组环,必须
	 * @param flightDateStart 必须
	 * @param flightDateEnd 必须
	 * @param taskLoopId 必须
	 * @param dutyCode 必须
	 * @return
	 */
	@RequestMapping("autoCreateLinkLine")
	@ResponseBody
	public Object autoCreateLinkLine(@RequestParam(required = true) Date flightDateStart,
			@RequestParam(required = true) Date flightDateEnd,@RequestParam(required = true) int[] taskLoopId,
			@RequestParam(required = true) String dutyCode
			) {
		if (!fullCalendarService.flightDateCk(flightDateStart, flightDateEnd)) {
			return (ResponseResultBuilder.errorResult(null, "该日期不能执行操作", "400"));
		}
		taskLoopFlightFactService.autoTaskLoopFact(dutyCode, flightDateStart, flightDateEnd,taskLoopId);
		return (ResponseResultBuilder.successResult());
	}
	
	/**
	 * 查询某段时间范围内的航班号+起飞目的机场
	 * @param flightDateStart
	 * @param flightDateEnd
	 * @return
	 */
	@RequestMapping("getFlightNoList")
	@ResponseBody
	public Object getFlightNoList(@RequestParam(required = true) Date flightDateStart,
			@RequestParam(required = true) Date flightDateEnd) {
		List<Select2> list = select2Service.getFlightNoList(flightDateStart,flightDateEnd);
		return (list);
	}

}
